/**
 * 开发使用
 */
var gulp = require('gulp'),
    gulpLoadPlugins = require('gulp-load-plugins'),
    plugins = gulpLoadPlugins(),
    fs = require('fs'),
    baseConfig = require('./gulp/config'),
    modRewrite  = require('connect-modrewrite');

    plugins.browserSync = require('browser-sync').create();
    plugins.named = require('vinyl-named');
    plugins.del = require('del');
    plugins.path = require('path');
    webpack = require('webpack');
    DefinePlugin = require('./node_modules/webpack/lib/optimize/DedupePlugin');
    plugins.pngquant = require('imagemin-pngquant');

/**
 * 开发配置
 */
var config_dev = baseConfig({
    taskPrefix: '[dev]',
    dest:"./dev/", // 发布目录
    copyFiles:[],
    webpack:{
        watch: true,
        devtool: 'inline-source-map'
    }
});

/**
 * 发布配置
 */
var config_pro = baseConfig({
    taskPrefix: '[pro]',
    dest:"./dist/", // 发布目录
    copyFiles:[],
    reversion:true, // reversion
    webpack:{
        plugins:[
            new webpack.DefinePlugin({
                'process.env': {
                    NODE_ENV: '"production"'
                }
            }),
            new webpack.optimize.UglifyJsPlugin({
                // preserveComments:'all',
                compress: {
                    warnings: false
                }
            })
        ]
    }
});

var _task = require("./gulp/task"),
    develop_task = _task(gulp, plugins, config_dev);
    production_task = _task(gulp, plugins, config_pro);

// webServer
gulp.task('browser', function () {
    var proxy = "http://localhost:8900",
        rulePath = [],
        proxyRule = [];
    
    rulePath.forEach(function (rule) {
        proxyRule.push( '^/'+ rule +'/(.*)$ '+proxy+'/'+ rule +'/$1 [P]' );
    });

    plugins.browserSync.init({
        // open: "ui",
        open:false,
        reloadDebounce:1000,
        reloadDelay: 0,
        notify: {      //自定制livereload 提醒条
            styles: [
                "margin: 0",
                "padding: 5px",
                "position: fixed",
                "font-size: 10px",
                "z-index: 9999",
                "bottom: 0px",
                "right: 0px",
                "border-radius: 0",
                "border-top-left-radius: 5px",
                "color: white",
                "text-align: center"
            ]
        },
        startPath:'/html/index.html',
        server: {
            baseDir: "./dev/",
            middleware: [
                modRewrite(proxyRule)
            ]
        }
    });

    plugins.browserSync.watch('./dev/**/*', plugins.browserSync.reload);

});

//开发编译
gulp.task(
    'build_develop',
    gulp.series(
        develop_task.cleanDest
        ,gulp.parallel(
            develop_task.copyImg
            ,develop_task.copyFiles
            ,develop_task.copyMedia
            ,develop_task.copyFonts
            ,develop_task.copyVendors
            ,develop_task.renderLess
            ,develop_task.develop.copyHtml
        )
        ,develop_task.webpack
        ,develop_task.develop.watch
    )
);

// 发布编译
gulp.task(
    'build_production',
    gulp.series(
        production_task.cleanDest
        ,gulp.parallel(
            production_task.copyImg
            ,production_task.copyFiles
            ,production_task.copyMedia
            ,production_task.copyFonts
            ,production_task.production.renderSrcLess
            ,production_task.webpack
        )
        ,gulp.parallel(
            production_task.production.html
        )
        ,production_task.production.reversion
    )
);

